﻿/**
 * jQuery EasyUI 1.4
 * 
 * Copyright (c) 2009-2014 www.jeasyui.com. All rights reserved.
 * 
 * Licensed under the GPL license: http://www.gnu.org/licenses/gpl.txt To use it
 * on other terms please contact us at info@jeasyui.com
 * 
 */
(function($) {
	function validateboxAddClas(target) {
		$(target).addClass("validatebox-text");
	};
	function destroy(target) {
		var validatebox = $.data(target, "validatebox");
		validatebox.validating = false;
		if (validatebox.timer) {
			clearTimeout(validatebox.timer);
		}
		$(target).tooltip("destroy");
		$(target).unbind();
		$(target).remove();
	};
	function bindEvent(target) {
		var opts = $.data(target, "validatebox").options;
		var box = $(target);
		box.unbind(".validatebox");
		if (opts.novalidate || box.is(":disabled")) {
			return;
		}
		for ( var e in opts.events) {
			$(target).bind(e + ".validatebox", {
				target : target
			}, opts.events[e]);
		}
	};
	function focusEvent(e) {
		var target = e.data.target;
		var validatebox = $.data(target, "validatebox");
		var box = $(target);
		if ($(target).attr("readonly")) {
			return;
		}
		validatebox.validating = true;
		validatebox.value = undefined;
		(function() {
			if (validatebox.validating) {
				if (validatebox.value != box.val()) {
					validatebox.value = box.val();
					if (validatebox.timer) {
						clearTimeout(validatebox.timer);
					}
					validatebox.timer = setTimeout(function() {
						$(target).validatebox("validate");
					}, validatebox.options.delay);
				} else {
					repositionTooltip(target);
				}
				setTimeout(arguments.callee, 200);
			}
		})();
	}
	;
	function blurEvent(e) {
		var target = e.data.target;
		var validatebox = $.data(target, "validatebox");
		if (validatebox.timer) {
			clearTimeout(validatebox.timer);
			validatebox.timer = undefined;
		}
		validatebox.validating = false;
		hideTooltip(target);
	};
	function mouseenterEvent(e) {
		var target = e.data.target;
		if ($(target).hasClass("validatebox-invalid")) {
			showTooltip(target);
		}
	};
	function mouseleaveEvent(e) {
		var target = e.data.target;
		var validatebox = $.data(target, "validatebox");
		if (!validatebox.validating) {
			hideTooltip(target);
		}
	}
	;
	function showTooltip(target) {
		var validatebox = $.data(target, "validatebox");
		var opts = validatebox.options;
		$(target).tooltip($.extend({}, opts.tipOptions, {
			cls:"error-tooltip",
			content : validatebox.message,
			position : opts.tipPosition,
			deltaX : opts.deltaX
		})).tooltip("show");
		validatebox.tip = true;
	};
	function repositionTooltip(target) {
		var validatebox = $.data(target, "validatebox");
		if (validatebox && validatebox.tip) {
			$(target).tooltip("reposition");
		}
	};
	function hideTooltip(target) {
		var validatebox = $.data(target, "validatebox");
		validatebox.tip = false;
		$(target).tooltip("hide");
	};
	function validate(target) {
		var validatebox = $.data(target, "validatebox");
		var opts = validatebox.options;
		var box = $(target);
		opts.onBeforeValidate.call(target);
		var _25 = _26();
		opts.onValidate.call(target, _25);
		return _25;
		function _setMessage(msg) {
			validatebox.message = msg;
		};
		function _28(validName, rulesValue) {
			var boxValue = box.val();
			var _validName = /([a-zA-Z_]+)(.*)/.exec(validName);
			var _rulesName = opts.rules[_validName[1]];
			if (_rulesName && boxValue) {
				var _rulesValue = rulesValue || opts.validParams || eval(_validName[2]);
				if (!_rulesName["validator"].call(target, boxValue, _rulesValue)) {
					box.addClass("validatebox-invalid");
					var message = _rulesName["message"];
					if (_rulesValue) {
						for (var i = 0; i < _rulesValue.length; i++) {
							message = message.replace(new RegExp("\\{" + i + "\\}", "g"), _rulesValue[i]);
						}
					}
					_setMessage(opts.invalidMessage || message);
					if (validatebox.validating) {
						showTooltip(target);
					}
					return false;
				}
			}
			return true;
		};
		function _26() {
			box.removeClass("validatebox-invalid");
			hideTooltip(target);
			if (opts.novalidate || box.is(":disabled")) {
				return true;
			}
			if (opts.required) {
				if (box.val() == "" || box.val() == "--\u8BF7\u9009\u62E9--") {
					box.addClass("validatebox-invalid");
					_setMessage(opts.missingMessage);
					if (validatebox.validating) {
						showTooltip(target);
					}
					return false;
				}
			}
			if (opts.validType) {
				if ($.isArray(opts.validType)) {
					for (var i = 0; i < opts.validType.length; i++) {
						if (!_28(opts.validType[i])) {
							return false;
						}
					}
				} else {
					if (typeof opts.validType == "string") {
						if (!_28(opts.validType)) {
							return false;
						}
					} else {
						for ( var validName in opts.validType) {
							var _31 = opts.validType[validName];
							if (!_28(validName, _31)) {
								return false;
							}
						}
					}
				}
			}
			return true;
		};
	};
	function setValidationStatus(target, status) {
		var opts = $.data(target, "validatebox").options;
		if (status != undefined) {
			opts.novalidate = status;
		}
		if (opts.novalidate) {
			$(target).removeClass("validatebox-invalid");
			hideTooltip(target);
		}
		validate(target);
		bindEvent(target);
	};
	$.fn.validatebox = function(options, param) {
		if (typeof options == "string") {
			return $.fn.validatebox.methods[options](this, param);
		}
		options = options || {};
		return this.each(function() {
			var validatebox = $.data(this, "validatebox");
			if (validatebox) {
				$.extend(validatebox.options, options);
			} else {
				validateboxAddClas(this);
				$.data(this, "validatebox", {
					options : $.extend({}, $.fn.validatebox.defaults, $.fn.validatebox.parseOptions(this), options)
				});
			}
			setValidationStatus(this);
			validate(this);
		});
	};
	$.fn.validatebox.methods = {
		options : function(jq) {
			return $.data(jq[0], "validatebox").options;
		},
		destroy : function(jq) {
			return jq.each(function() {
				destroy(this);
			});
		},
		validate : function(jq) {
			return jq.each(function() {
				validate(this);
			});
		},
		isValid : function(jq) {
			return validate(jq[0]);
		},
		enableValidation : function(jq) {
			return jq.each(function() {
				setValidationStatus(this, false);
			});
		},
		disableValidation : function(jq) {
			return jq.each(function() {
				setValidationStatus(this, true);
			});
		}
	};
	$.fn.validatebox.parseOptions = function(target) {
		var t = $(target);
		return $.extend({}, $.parser.parseOptions(target, [ "validType",
				"missingMessage", "invalidMessage", "tipPosition", {
					delay : "number",
					deltaX : "number"
				} ]), {
			required : (t.attr("required") ? true : undefined),
			novalidate : (t.attr("novalidate") != undefined ? true : undefined)
		});
	};
	$.fn.validatebox.defaults = {
		required : false,
		validType : null,
		validParams : null,
		delay : 200,
		missingMessage : "本字段为必填项.",
		invalidMessage : null,
		tipPosition : "right",
		deltaX : 0,
		novalidate : false,
		events : {
			focus : focusEvent,
			blur : blurEvent,
			mouseenter : mouseenterEvent,
			mouseleave : mouseleaveEvent,
			click : function(e) {
				var t = $(e.data.target);
				if (!t.is(":focus")) {
					t.trigger("focus");
				}
			}
		},
		tipOptions : {
			showEvent : "none",
			hideEvent : "none",
			showDelay : 0,
			hideDelay : 0,
			zIndex : "",
			onShow : function() {
//				$(this).tooltip("tip").css({
//					color : "#000",
//					borderColor : "#CC9933",
//					backgroundColor : "#FFFFCC"
//				});
			},
			onHide : function() {
				$(this).tooltip("destroy");
			}
		},
		rules : {
			email : {
				validator : function(_3a) {
					return /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(_3a);
				},
				//message : "Please enter a valid email address."
				message : "\u8BF7\u8F93\u5165\u6709\u6548\u7684\u90AE\u4EF6\u5730\u5740"
			},
			url : {
				validator : function(_3b) {
					return /^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(_3b);
				},
				//message : "Please enter a valid URL."
				message : "\u8BF7\u8F93\u5165\u6709\u6548\u7684URL"
			},
			length : {
				validator : function(_3c, _3d) {
					var len = $.trim(_3c).length;
					return len >= _3d[0] && len <= _3d[1];
				},
				//message : "Please enter a value between {0} and {1}."
				message : "\u957F\u5EA6\u9700\u8981\u5728{0}-{1}\u4E4B\u95F4"
			},
			remote : {
				validator : function(_3e, _3f) {
					var _40 = {};
					_40[_3f[1]] = _3e;
					var _41 = $.ajax({
						url : _3f[0],
						dataType : "json",
						data : _40,
						async : false,
						cache : false,
						type : "post"
					}).responseText;
					return _41 == "true";
				},
				message : "Please fix this field."
			}
		},
		onBeforeValidate : function() {
		},
		onValidate : function(_42) {
		}
	};
})(jQuery);
